package com.extension.company.view.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.extension.company.admin.R;
import com.extension.company.common.utils.DataUtils;
import com.extension.company.model.entity.GoodsInfoDetail2;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者:zwk
 * 联系方式：497981239@qq.com
 * 时间：on 2019/7/30 21:48
 * 功能：
 */
public class CommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

	public static final int NO_IMAGE = 0; // 无图片
	public static final int HAS_IMAGE = 1; // 有图片

	private Context context;
	private List<GoodsInfoDetail2.DataBean.GoodsCommentBean> list;

	public CommentAdapter(Context context, List<GoodsInfoDetail2.DataBean.GoodsCommentBean> list) {
		this.context = context;
		this.list = list;
	}

	@NonNull
	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
		View view;
		if (i == NO_IMAGE) {
			view = LayoutInflater.from(context).inflate(R.layout.comment_item, viewGroup, false);
			view.findViewById(R.id.comment_item_recycle).setVisibility(View.GONE);
			return new NoImageHolder(view);
		} else if (i == HAS_IMAGE) {
			view = LayoutInflater.from(context).inflate(R.layout.comment_item, viewGroup, false);
			return new CommenHolder(view);
		}
		return null;
	}


	@Override
	public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
		if (viewHolder instanceof CommenHolder) {
			CommenHolder commenHolder = (CommenHolder) viewHolder;
			Glide.with(context).load(list.get(i).getAvatar()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(commenHolder.commentUserImg);
			commenHolder.commentTime.setText(DataUtils.formatDateTime(list.get(i).getCreatetime()));
			commenHolder.commentComment.setText(list.get(i).getContent());
			String[] commentImg = list.get(i).getImages().split(",");
			if (commentImg.length > 0) { // 有评论图片的时候
				CommentItemImgeAdapter adapter = new CommentItemImgeAdapter(context, commentImg);
				commenHolder.commentItemRecycle.setLayoutManager(new GridLayoutManager(context, 3));
				commenHolder.commentItemRecycle.setAdapter(adapter);
			}
		} else if (viewHolder instanceof NoImageHolder) {
			NoImageHolder noImageHolder = (NoImageHolder) viewHolder;
			Glide.with(context).load(list.get(i).getAvatar()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(noImageHolder.commentUserImg);
			noImageHolder.commentTime.setText(DataUtils.formatDateTime(list.get(i).getCreatetime()));
			noImageHolder.commentComment.setText(list.get(i).getContent());
		}


	}

	@Override
	public int getItemCount() {
		return list.size();
	}

	@Override
	public int getItemViewType(int position) {
		if ("1".equals(list.get(position).getIs_img())) {
			return HAS_IMAGE;
		} else if ("0".equals(list.get(position).getIs_img())) {
			return NO_IMAGE;
		}
		return NO_IMAGE;
	}

	class CommenHolder extends RecyclerView.ViewHolder {
		@BindView(R.id.comment_user_img)
		ImageView commentUserImg;
		@BindView(R.id.comment_user_name)
		TextView commentUserName;
		@BindView(R.id.comment_comment)
		TextView commentComment;
		@BindView(R.id.comment_item_recycle)
		RecyclerView commentItemRecycle;
		@BindView(R.id.comment_fragment_time)
		TextView commentTime;

		CommenHolder(@NonNull View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
		}
	}

	class NoImageHolder extends RecyclerView.ViewHolder {
		@BindView(R.id.comment_user_img)
		ImageView commentUserImg;
		@BindView(R.id.comment_user_name)
		TextView commentUserName;
		@BindView(R.id.comment_comment)
		TextView commentComment;
		@BindView(R.id.comment_item_recycle)
		RecyclerView commentItemRecycle;
		@BindView(R.id.comment_fragment_time)
		TextView commentTime;

		public NoImageHolder(@NonNull View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
		}
	}
}
